<?php
/*
Plugin Name: 网站基本设置
Plugin URI: /wp-admin/admin.php?page=site-basic-setting
Description: 自用，授之以鱼不如授之以渔
Version: 1.0.0
Author: 某喵的光
Author URI: http://www.yusunny.com/
*/
// Make sure we don't expose any info if called directly
if ( basename( $_SERVER['PHP_SELF'] ) == basename( __FILE__ ) ) {
	die( 'Sorry, but you cannot access this page directly.' );
}

add_action('admin_menu', 'site_basic_setting');
function site_basic_setting() {
	add_menu_page('网站设置', '网站基本设置', 'manage_options', 'site-basic-setting', 'site_basic_setting_page');
}

// 加一个新页面
include_once("custom-option-function.php");
include_once("custom-option-images.php");

// Retrieves plugin options if they exist or returns default values if not

function basic_getoptions() {
	if( get_option( 'basic_options' )) {
		$basic_options = get_option( 'basic_options' );
		if( !isset( $basic_options['basic_fontsize_html'] ) ){
			$basic_options['basic_fontsize_html'] = "";
		}
		if( !isset( $basic_options['basic_fontsize_body'] ) ){
			$basic_options['basic_fontsize_body'] = "";
		}
		if( !isset( $basic_options['basic_fontsize_h1'] ) ){
			$basic_options['basic_fontsize_h1'] = "";
		}
		if( !isset( $basic_options['basic_fontsize_h2'] ) ){
			$basic_options['basic_fontsize_h2'] = "";
		}
		if( !isset( $basic_options['basic_fontsize_h3'] ) ){
			$basic_options['basic_fontsize_h3'] = "";
		}
		if( !isset( $basic_options['basic_fontsize_h4'] ) ){
			$basic_options['basic_fontsize_h4'] = "";
		}
		if( !isset( $basic_options['basic_fontsize_h5'] ) ){
			$basic_options['basic_fontsize_h5'] = "";
		}
		if( !isset( $basic_options['basic_fontsize_h6'] ) ){
			$basic_options['basic_fontsize_h6'] = "";
		}
		if( !isset( $basic_options['basic_fontsize_widget'] ) ){
			$basic_options['basic_fontsize_widget'] = "";
		}
		if( !isset( $basic_options['basic_fontcolor_body'] ) ){
			$basic_options['basic_fontcolor_body'] = "";
		}
		if( !isset( $basic_options['basic_fontcolor_h1'] ) ){
			$basic_options['basic_fontcolor_h1'] = "";
		}
		if( !isset( $basic_options['basic_fontcolor_h2'] ) ){
			$basic_options['basic_fontcolor_h2'] = "";
		}
		if( !isset( $basic_options['basic_fontcolor_h3'] ) ){
			$basic_options['basic_fontcolor_h3'] = "";
		}
		if( !isset( $basic_options['basic_fontcolor_h4'] ) ){
			$basic_options['basic_fontcolor_h4'] = "";
		}
		if( !isset( $basic_options['basic_fontcolor_h5'] ) ){
			$basic_options['basic_fontcolor_h5'] = "";
		}
		if( !isset( $basic_options['basic_fontcolor_h6'] ) ){
			$basic_options['basic_fontcolor_h6'] = "";
		}
		if( !isset( $basic_options['basic_fontcolor_widget'] ) ){
			$basic_options['basic_fontcolor_widget'] = "";
		}
	} else {
		$basic_options['basic_fontsize_html'] = '';
		$basic_options['basic_fontsize_body'] = '';
		$basic_options['basic_fontsize_h1'] = '';
		$basic_options['basic_fontsize_h2'] = '';
		$basic_options['basic_fontsize_h3'] = '';
		$basic_options['basic_fontsize_h4'] = '';
		$basic_options['basic_fontsize_h5'] = '';
		$basic_options['basic_fontsize_h6'] = '';
		$basic_options['basic_fontsize_widget'] = '';
		$basic_options['basic_fontcolor_body'] = '';
		$basic_options['basic_fontcolor_h1'] = '';
		$basic_options['basic_fontcolor_h2'] = '';
		$basic_options['basic_fontcolor_h3'] = '';
		$basic_options['basic_fontcolor_h4'] = '';
		$basic_options['basic_fontcolor_h5'] = '';
		$basic_options['basic_fontcolor_h6'] = '';
		$basic_options['basic_fontcolor_widget'] = '';
	}
	return $basic_options;
}

 function site_basic_setting_page() {
 ?>
	<div class="wrap">
        <?php settings_errors(); ?>
        <div class="custom_option">
			<form method="post" action="options.php"  class="basic-form-setting">
				<?php
					settings_fields('basic_setting_url');
					do_settings_sections( 'basic_layoutsection' );
				?>
				<br>
				<button type="submit" name="submit">保存设置</button>
			</form>
		</div>
	</div>
 <?php }

 add_action( 'admin_init', 'basic_register_settings' );

 function basic_register_settings(){
	// register_setting 最少需要两个字段，第一个字段用在form中，第二个字段是数据库的字段名；
	// 因此不要在别的页面重复保存数据到basic_options中，否则可能会丢失
	register_setting( 'basic_setting_url', 'basic_options' );
	// 注册一个section
	add_settings_section( 'basic_layout_section', '样式区域', 'basic_layouttext', 'basic_layoutsection' );
	// 给该section注册一些字段
	add_settings_field( 'basic_css_html', 'html适用样式', 'basic_css_html_echo', 'basic_layoutsection', 'basic_layout_section' );
	add_settings_field( 'basic_css_body', 'body适用样式', 'basic_css_body_echo', 'basic_layoutsection', 'basic_layout_section' );
	add_settings_field( 'basic_css_h1', 'H1字体样式', 'basic_css_h1_echo', 'basic_layoutsection', 'basic_layout_section' );
	add_settings_field( 'basic_css_h2', 'H2字体样式', 'basic_css_h2_echo', 'basic_layoutsection', 'basic_layout_section' );
	add_settings_field( 'basic_css_h3', 'H3字体样式', 'basic_css_h3_echo', 'basic_layoutsection', 'basic_layout_section' );
	add_settings_field( 'basic_css_h4', 'H4字体样式', 'basic_css_h4_echo', 'basic_layoutsection', 'basic_layout_section' );
	add_settings_field( 'basic_css_h5', 'H5字体样式', 'basic_css_h5_echo', 'basic_layoutsection', 'basic_layout_section' );
	add_settings_field( 'basic_css_h6', 'H6字体样式', 'basic_css_h6_echo', 'basic_layoutsection', 'basic_layout_section' );
	add_settings_field( 'basic_css_widget', 'Widget适用样式', 'basic_css_widget_echo', 'basic_layoutsection', 'basic_layout_section' );

}

function basic_layouttext(){?>
	<p class="description">包括字体大小，颜色，禁用默认冗余功能</p>
<?php }

function basic_css_html_echo() {
	$options = basic_getoptions();
	$current_size = $options['basic_fontsize_html'];
?>
	<input type="text" name="basic_options[basic_fontsize_html]" value="<?php echo esc_attr($current_size); ?>" placeholder="字体大小">
<?php }

function basic_css_body_echo() {
	$options = basic_getoptions();
	$current_size = $options['basic_fontsize_body'];
	$current_color = $options['basic_fontcolor_body'];
?>
	<input type="text" name="basic_options[basic_fontsize_body]" value="<?php echo esc_attr($current_size); ?>" placeholder="字体大小">
	<input type="text" name="basic_options[basic_fontcolor_body]" value="<?php echo esc_attr($current_color); ?>" placeholder="字体颜色">
<?php }

function basic_css_h1_echo() {
	$options = basic_getoptions();
	$current_size = $options['basic_fontsize_h1'];
	$current_color = $options['basic_fontcolor_h1'];
?>
	<input type="text" name="basic_options[basic_fontsize_h1]" value="<?php echo esc_attr($current_size); ?>" placeholder="字体大小">
	<input type="text" name="basic_options[basic_fontcolor_h1]" value="<?php echo esc_attr($current_color); ?>" placeholder="字体颜色">
<?php }

function basic_css_h2_echo() {
	$options = basic_getoptions();
	$current_size = $options['basic_fontsize_h2'];
	$current_color = $options['basic_fontcolor_h2'];
?>
	<input type="text" name="basic_options[basic_fontsize_h2]" value="<?php echo esc_attr($current_size); ?>" placeholder="字体大小">
	<input type="text" name="basic_options[basic_fontcolor_h2]" value="<?php echo esc_attr($current_color); ?>" placeholder="字体颜色">

<?php }

function basic_css_h3_echo() {
	$options = basic_getoptions();
	$current_size = $options['basic_fontsize_h3'];
	$current_color = $options['basic_fontcolor_h3'];
?>
	<input type="text" name="basic_options[basic_fontsize_h3]" value="<?php echo esc_attr($current_size); ?>" placeholder="字体大小">
	<input type="text" name="basic_options[basic_fontcolor_h3]" value="<?php echo esc_attr($current_color); ?>" placeholder="字体颜色">

<?php }

function basic_css_h4_echo() {
	$options = basic_getoptions();
	$current_size = $options['basic_fontsize_h4'];
	$current_color = $options['basic_fontcolor_h4'];
?>
	<input type="text" name="basic_options[basic_fontsize_h4]" value="<?php echo esc_attr($current_size); ?>" placeholder="字体大小">
	<input type="text" name="basic_options[basic_fontcolor_h4]" value="<?php echo esc_attr($current_color); ?>" placeholder="字体颜色">

<?php }

function basic_css_h5_echo() {
	$options = basic_getoptions();
	$current_size = $options['basic_fontsize_h5'];
	$current_color = $options['basic_fontcolor_h5'];
?>
	<input type="text" name="basic_options[basic_fontsize_h5]" value="<?php echo esc_attr($current_size); ?>" placeholder="字体大小">
	<input type="text" name="basic_options[basic_fontcolor_h5]" value="<?php echo esc_attr($current_color); ?>" placeholder="字体颜色">

<?php }

function basic_css_h6_echo() {
	$options = basic_getoptions();
	$current_size = $options['basic_fontsize_h6'];
	$current_color = $options['basic_fontcolor_h6'];
?>
	<input type="text" name="basic_options[basic_fontsize_h6]" value="<?php echo esc_attr($current_size); ?>" placeholder="字体大小">
	<input type="text" name="basic_options[basic_fontcolor_h6]" value="<?php echo esc_attr($current_color); ?>" placeholder="字体颜色">

<?php }

function basic_css_widget_echo() {
	$options = basic_getoptions();
	$current_size = $options['basic_fontsize_widget'];
	$current_color = $options['basic_fontcolor_widget'];
?>
	<input type="text" name="basic_options[basic_fontsize_widget]" value="<?php echo esc_attr($current_size); ?>" placeholder="字体大小">
	<input type="text" name="basic_options[basic_fontcolor_widget]" value="<?php echo esc_attr($current_color); ?>" placeholder="字体颜色">

<?php }


// 处理保存的样式数据
function enqueue_styles_basic() {
	// 一定要有下面两行，不然不生效
    $my_basic_style = 'my-basic-style';
	wp_enqueue_style( $my_basic_style, get_template_directory_uri() . '/style.css' );
	// 开始处理数据
	$basic_options = get_option('basic_options');
	// 如果不写这个if，刚激活的时候会有报错，当然在保存之后就好了。
	if ($basic_options) {
		if (array_key_exists("basic_fontsize_html", $basic_options)) {
			$size_value = $basic_options["basic_fontsize_html"];
			$custom_inline_style_html_size = 'html'." { font-size: " . $size_value . "; }";
			wp_add_inline_style( $my_basic_style, $custom_inline_style_html_size );
		}
		if (array_key_exists("basic_fontsize_body", $basic_options)) {
			$size_value = $basic_options["basic_fontsize_body"];
			$custom_inline_style_body_size = 'body'." { font-size: " . $size_value . "; }";
			wp_add_inline_style( $my_basic_style, $custom_inline_style_body_size );
		}
		if (array_key_exists("basic_fontcolor_body", $basic_options)) {
			$color_value = $basic_options["basic_fontcolor_body"];
			$custom_inline_style_body_color = 'body'." { color: " . $color_value . "; }";
			wp_add_inline_style( $my_basic_style, $custom_inline_style_body_color );
		}
		// h1-h6干脆就循环写了
		for ($i=1; $i < 7; $i++) {
			if (array_key_exists("basic_fontsize_h".$i, $basic_options)) {
				$size_value = $basic_options["basic_fontsize_h".$i];
				$custom_inline_style_size = 'h'.$i.', .h'.$i." { font-size: " . $size_value . "; }";
				wp_add_inline_style( $my_basic_style, $custom_inline_style_size );
			}
			if (array_key_exists("basic_fontcolor_h".$i, $basic_options)) {
				$color_value = $basic_options["basic_fontcolor_h".$i];
				$custom_inline_style_color = 'h'.$i.', .h'.$i.', h'.$i.' a'." { color: " . $color_value . "; }";
				wp_add_inline_style( $my_basic_style, $custom_inline_style_color );
			}
		}
		if (array_key_exists("basic_fontsize_widget", $basic_options)) {
			$size_value = $basic_options["basic_fontsize_widget"];
			$custom_inline_style_widget_size = '.widget-title'." { font-size: " . $size_value . "; }";
			wp_add_inline_style( $my_basic_style, $custom_inline_style_widget_size );
		}
		if (array_key_exists("basic_fontcolor_widget", $basic_options)) {
			$color_value = $basic_options["basic_fontcolor_widget"];
			$custom_inline_style_widget_color = '.widget-title'." { color: " . $color_value . "; }";
			wp_add_inline_style( $my_basic_style, $custom_inline_style_widget_color );
		}
	}
}
add_action( 'wp_enqueue_scripts', 'enqueue_styles_basic' );

//Add js in plugin

add_action( 'admin_print_scripts', 'basic_option_scripts' );
/**
 * Enqueues javascript files and css
 */
function basic_option_scripts() {
	wp_enqueue_media();
	wp_enqueue_script( 'basic_option_js', plugins_url('js/cod.js?v='.time() , __FILE__ ), array( 'jquery', 'media-upload', 'thickbox' ) );
}

add_action( 'admin_print_styles', 'basic_styles_admin' );

function basic_styles_admin() {
	wp_enqueue_style( 'stylesheet', plugins_url('css/cod.css' , __FILE__ ), array(), '', false );
}